// 全局共享数据示例
import { getAllRedBallList } from '@/pages/allRedBoll';
import { getHistoryList } from '@/pages/historyResult';
import { useEffect, useState } from 'react';
import {
  sampleSize, //获得随机数
  uniq, //去重
  sortedUniq,//去重并排序
  take, //从0开始获得指定数组
  takeRight,
  max,
  min,
  indexOf,
  lastIndexOf,
  countBy,
  reduce,
  orderBy,//排序
  slice//裁剪数组
} from "lodash"
window.uniq = uniq;

const bollInfo = () => {
  const [allRedBallList, setAllRedBallList] = useState<any>([]);//所有结果数组形式
  const [allRedBallStrList, setAllRedBallStrList] = useState<any>([]);//所有结果字符串形式

  const [historyList, setHistoryList] = useState<{}>({});//历史结果-所有信息
  const [historyResultStrList, setHistoryResultStrList] = useState<[]>([]);//历史结果-字符串形式
  const [historyOddEvenList, setHistoryOddEvenList] = useState<[]>([]);//历史红球奇偶组合
  const [historyOddEvetTongj, setHistoryOddEvetTongj] = useState({});//历史红球奇偶组合
  

  useEffect(()=>{
    let historyData = getHistoryList();//历史记录数据
    let allBall = getAllRedBallList();//所有红球结果
    
    //给历史记录增加locationIndex属性
    historyData.historyResultStrList.forEach((redBallStr:string)=>{
      let historyRedBall = historyData.historyResultList[redBallStr];
      historyRedBall.locationIndex = allBall.allRedBollStr.indexOf(redBallStr);
    })

    let tongji = getHistoryOddTongji({
      historyResultList: historyData.historyResultList,
      historyResultStrList: historyData.historyResultStrList,
      historyOddEvenList: historyData.historyOddEvenList
    })
    
    setAllRedBallList(allBall.allRedBallList);//设置所有红球结果数组形式
    setAllRedBallStrList(allBall.allRedBollStr);//设置所有结果字符串形式
    setHistoryList(tongji.historyResultList);
    setHistoryResultStrList(historyData.historyResultStrList);
    setHistoryOddEvenList(historyData.historyOddEvenList);//红球奇偶组合
    setHistoryOddEvetTongj(tongji);

  },[])

  return {
    allRedBallList,
    allRedBallStrList,
    historyList,
    historyResultStrList,
    historyOddEvenList,
    historyOddEvetTongj
  };
};

export default bollInfo;


function getHistoryOddTongji({
  historyResultList,
  historyResultStrList,
  historyOddEvenList
}:any){
    // historyResultStrList是倒序排列，最新的一期在第一个
    let keyList = historyResultStrList.concat().reverse();//把结果翻转一下
    let oddList:any = historyOddEvenList.concat().reverse();//记录每个历史记录的奇偶比
    let list:any = [];//缓存结果写入state
    let currentJiangeCountList:any = [];//每期奇偶间隔次数列表，会重复，用来计算间隔的频率
    let oddEvenObjList:any = {};//以奇偶情况作为key，记录每个奇偶情况出现的频率
    
    keyList.map((key, index)=>{
      let item = historyResultList[key];
      let currentOddEven = item.oddEven;
      let leftList = take(oddList, index);//查找距离上一次出现的次数，需要截取从当前期数往第1期的数组
      let oddEvenLastIndex = lastIndexOf(leftList, currentOddEven);//然后从右往左，找到的第一个index，就是距离的期数
      
      item.oddEvenLastIndex = leftList.length - 1 - oddEvenLastIndex;//总长度 - 1 - 最后一次出现的索引 = 距离上一次出现的期数
      if(oddEvenLastIndex == -1){
        item.oddEvenLastIndex = 0;
      }
      currentJiangeCountList.push(item.oddEvenLastIndex);//记录每期奇偶间隔次数


      if(oddEvenObjList[currentOddEven] === undefined){
        oddEvenObjList[currentOddEven] = 1;
      }else{
        oddEvenObjList[currentOddEven] = oddEvenObjList[currentOddEven] + 1;
      }
    });

    //currentJiangeCountList 每期奇偶间隔次数列表，会重复，用来计算间隔的频率
    //奇偶组合距离上次出现的期数距离作为key，出现多少次作为值 -> {奇偶组合距离上次出现的期数:出现次数}
    let sortListObj = countBy(currentJiangeCountList)//统计元素数组出现的次数

    return {
        sortListObj: sortListObj,//奇偶组合距离上次出现的期数距离作为key，出现多少次作为值
        oddEvenObjList: oddEvenObjList,
        historyResultList
    }
}